package DomainModel;

/**
 *
 * @author PRATAP, Reynier
 */
public class Grade {
    private int gradeId;
    private ClassWork classWork;
    private int classWorkId;
    private int pointsEarned;
    private User student;
    private LetterGrade letterGrade;
    private String comments;
    
    public Grade(int pGradeId, ClassWork pClassWork, int pPointsEarned, User pStudent, LetterGrade pLetterGrade, String pComments){
        this.gradeId = pGradeId;
        this.classWork = pClassWork;
        this.pointsEarned = pPointsEarned;
        this.student = pStudent;
        this.letterGrade = pLetterGrade;
        this.comments = pComments;
    }
    
    public Grade(int pGradeId, ClassWork pClassWork, int pPointsEarned, User pStudent, String pComments){
        this.gradeId = pGradeId;
        this.classWork = pClassWork;
        this.pointsEarned = pPointsEarned;
        this.student = pStudent;
        this.comments = pComments;
        
        if (classWork != null) {
            classWorkId = pClassWork.getClassWorkId();
        }
    }
    
    public Grade(int pGradeId, int pClassWorkId, int pPointsEarned, User pStudent, LetterGrade pLetterGrade, String pComments){
        this.gradeId = pGradeId;
        this.classWorkId = pClassWorkId;
        this.pointsEarned = pPointsEarned;
        this.student = pStudent;
        this.letterGrade = pLetterGrade;
        this.comments = pComments;
    }
        
    public int getGradeId() {
        return gradeId;
    }
    
    public ClassWork getClassWork(){
        return classWork;
    }
    
    public int getPointsEarned() {
        return pointsEarned;
    }
    
    public User getStudent(){
        return student;
    }
    
    public LetterGrade getLetterGrade() {
        return letterGrade;
    }
    
    public String getComments(){
        return comments;
    }
    
    public void setGradeId(int gradeId) {
        this.gradeId = gradeId;
    }

    public int getClassWorkId() {
        return classWorkId;
    }

    public void setClassWorkId(int classWorkId) {
        this.classWorkId = classWorkId;
    }
   
    public LetterGrade calculateLetterGrade() {
        if (classWork == null) {
            return null;
        }
        int percent = (pointsEarned * 100) / classWork.getMaxPoints();
        
        if (percent >= 90) return new LetterGrade(1, "A");
        if (percent >= 87) return new LetterGrade(2, "A-");
        if (percent >= 84) return new LetterGrade(3, "B+");
        if (percent >= 80) return new LetterGrade(4, "B");
        if (percent >= 77) return new LetterGrade(5, "B-");
        if (percent >= 74) return new LetterGrade(6, "C+");
        if (percent >= 70) return new LetterGrade(7, "C");
        if (percent >= 65) return new LetterGrade(8, "C-");
        if (percent >= 60) return new LetterGrade(9, "D+");
        if (percent >= 55) return new LetterGrade(10, "D");
        if (percent >= 50) return new LetterGrade(11, "D-");
        
        return new LetterGrade(12, "F");
    }
    
    public String validate() {
        if ((classWork != null) && (pointsEarned > classWork.getMaxPoints())) {
           return "Grade(s) with points greather than the max points.";
        }
            
        return "";
    }
}
